﻿using huaxin_core;
using huaxin_Impl;
using huaxin_Interface;
using huaxin_Model;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin.MP.Containers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Senparc.Weixin.MP.Sample.CommonService.MessageHandlers.CustomMessageHandler
{
    public class MessageHelper
    {
        /// <summary>
        /// 关注
        /// </summary>
        /// <param name="openId"></param>
        /// <param name="shareOpenId"></param>
        public static string SubscribeEvent(string openId, string shareOpenId)
        {
            try
            {
                huaxin_IUsers userinfo = new huaxin_UserImpl();
                var user =userinfo.GetModel(openId);
                if (user != null && string.IsNullOrEmpty(openId))
                {
                    return user.NickName;
                }
                else
                {
                    var accessToken = AccessTokenContainer.GetAccessToken(WxConfig.AppId);
                    var userApi = UserApi.Info(accessToken, openId);
                    if (userApi != null)
                    {

                        huaxin_users users = new huaxin_users();
                        users.NickName = userApi.nickname;
                        users.HeadImgUrl = userApi.headimgurl;
                        users.OpenId = userApi.openid;
                        users.Sex = userApi.sex;
                        users.Country = userApi.country;
                        users.City = userApi.city;
                        userinfo.AddUser(users);
                        //userinfo.AddUser(userApi.headimgurl, userApi.nickname, userApi.openid, userApi.sex, shareOpenId);
                        return userApi.nickname;
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return "";
        }
    }
}
